<?php
/**
 * ClientcheckAction.class.php
 *
 * @package ClientcheckAction.class
 * @author lukeqin <ql2008@126.com>
 * @version 0.1
 * @copyright (C) 2014-10-03 08:11 lukeqin <ql2008@126.com>
 */

class ClientcheckAction extends Action{

	/**
	 * ���Ȩ��
	 */
	private function checkAuth($auth){
		if ( !D('User')->hasProperty($_REQUEST['sellername'],$auth) ) {
			echo json_encode(FALSE);
			exit();
		}		
	}
	function getIP4checkLocation( ){
		$key = 'getIP4checkLocation';
		if (S($key)) {
// 			echoJson(0);
// 			exit();
		}
		S($key,1,60);
		$this->checkAuth('checkIpLocation');
		$lists = D('Agent')->getIP4checkLocationMuti();
		$data = 0;
		if ( $lists ) {
			$data = array(
				'lists'=>$lists,
				'url'=>'http://ip.taobao.com/service/getIpInfo.php?ip=', 
				//'map'=>array(
					//'country'=>'agt_country',
					//'region'=>'agt_province',
					//'city'=>'agt_city', 
				//),
			);
		}
		echo json_encode($data);
	}

	function setIpLocation( ){
		$this->checkAuth('checkIpLocation');
		
		foreach ($_REQUEST['data'] as $id=>$res){
			$json = json_decode($res);
			if ( $json ) {
				if ( isset($json->code) and 0== $json->code and $json->data ) {
					//taobao api
					$data = $json->data;
					D('Agent')->where(array(
						'agt_id'=>$id,
						'agt_country'=>'', 
					))->save(array(
						'agt_country'=>$data->country,
						'agt_province'=>$data->region,
						'agt_city'=>$data->city, 
					));
					Log::write("set location success id is {$id}",Log::INFO);
				}else {
					Log::write('setIplocation err');
				}
			}
			else{
				Log::write('ClientcheckAction::setIplocation: parse Jason err jason string is '.$_REQUEST['res']);
			}
		}
		echoJson(1);
	}
	
	function report() {
		Log::write($_REQUEST['err']);
	}
}
